package com.hexu.event.dao;

import com.hexu.event.entity.Attractions;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AttractionsDao {

    //返回景点所有信息
    public List<Attractions> getAttractionsInformation(){
        List<Attractions> list = new ArrayList<>();
        String sql = "select * from attractions ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                Attractions attractions = new Attractions();
                attractions.setId(rs.getInt("id"));
                attractions.setName(rs.getString("name"));
                attractions.setOpenTime(rs.getString("open_time"));
                attractions.setTicketFare(rs.getInt("ticket_fare"));
                attractions.setTicketNumber(rs.getInt("ticket_number"));
                attractions.setIntroduce(rs.getString("introduce"));
                attractions.setCommentNumber(rs.getInt("comment_number"));

                list.add(attractions);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //返回景点数量
    public int attractionsNumber(){
        String sql = "select name from attractions ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            //使游标可滚动
            ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = ps.executeQuery();

            // 将游标移动到最后一行上
            rs.last();
            //获取景点数
            int attractionsCount = rs.getRow();
            //把光标移至第一行前
            rs.beforeFirst();

            return attractionsCount;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
       return 0;
    }

    //返回单个景点所有信息
    public Attractions getAttraction(String attractionName){
        String sql = "select * from attractions where name=? ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,attractionName);
            rs = ps.executeQuery();
            Attractions attractions = new Attractions();
            while (rs.next()){
                attractions.setId(rs.getInt("id"));
                attractions.setName(rs.getString("name"));
                attractions.setOpenTime(rs.getString("open_time"));
                attractions.setTicketFare(rs.getInt("ticket_fare"));
                attractions.setTicketNumber(rs.getInt("ticket_number"));
                attractions.setIntroduce(rs.getString("introduce"));
                attractions.setCommentNumber(rs.getInt("comment_number"));

            }
            return attractions;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //购票
    public boolean buyTicketAttractions(Attractions attractions){
        String sql = "update attractions set ticket_number=? where name=?";

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setInt(1,attractions.getTicketNumber());
            ps.setString(2, attractions.getName());
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //评论
    public boolean commentAttractions(Attractions attractions){
        String sql = "update attractions set comment_number=? where name=?";

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setInt(1,attractions.getCommentNumber());
            ps.setString(2, attractions.getName());
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }
}